<template>
  <div class="dialog">
    <el-dialog title="批量应用规则" width="30%" :visible.sync="dialogFormVisible">
      <div class="selected-box">
        <span class="text">课程名称</span>
        <el-select v-model="subjects" multiple filterable class="select-width" size="small" placeholder="请选择">
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submitFormInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";
import httpInterface from "../../utils/httpInterface";
import {errorMessage, successMessage, warningMessage} from "../../utils/validate";

export default {
  name: "BatchProcessModel",
  components: {},
  data() {
    return {
      dialogFormVisible: false,
      subjects: [],
      ruleId: ""
    }
  },

  computed: {
    ...mapState(["courseList"])
  },

  methods: {
    show(id) {
      this.dialogFormVisible = true;
      this.readerRuleInfo(id);
    },

    readerRuleInfo(id) {
      let _this = this;
      let params = {
        ruleId: id
      };
      httpInterface.requestGet(httpInterface.URL_RULE_GET_RULE_INFO, params, res => {
        let { code, body } = res.data;
        if (code == 200) {
          let { happy, calm, negative, sitting, prostrate, stand, stretch, raiseHands, ruleId, subInfo } = body;
          _this.happy = happy;
          _this.calm = calm;
          _this.negative = negative;
          _this.sitting = sitting;
          _this.prostrate = prostrate;
          _this.stand = stand;
          _this.stretch = stretch;
          _this.raiseHands = raiseHands;
          _this.ruleId = ruleId;

          let arr = [];
          subInfo.forEach(item => {
            arr.push(item.id);
          })
          _this.subjects = arr;
        } else {
          errorMessage("加载失败！");
        }
      }, null)
    },

    submitFormInfo() {
      let _this = this;
      let { subjects, happy, calm, negative, sitting, prostrate, stand, stretch, raiseHands, ruleId } = this;
      if (subjects.length == 0) return warningMessage("请选择课程！");

      let params = {
        subjects, ruleId, happy, calm, negative, sitting, prostrate, stand, stretch, raiseHands
      };
      httpInterface.requestPost(httpInterface.URL_RULE_UPDATE, params, res => {
        let { code } = res.data;
        if (code == 200) {
          _this.dialogFormVisible = false;
          successMessage("操作成功！");
          _this.$parent.getScoreRuleList();
        } else {
          errorMessage("操作失败！");
        }
      }, null, false)
    }
  }
}
</script>

<style scoped lang="scss">
.selected-box {
  text-align: center;

  .text {
    margin-right: 10px;
  }

  .select-width{
    width: 260px;
  }
}
</style>

